package utils;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.google.common.collect.Lists;

public class CreateDaoUtil {

	public static void main(String[] args) throws Exception {

		String rootPath = System.getProperty("user.dir");
		System.out.println(rootPath);

		File file = new File(rootPath + "/src/main/java/cn/com/platform/entity");
		String[] files = file.list();
		for (String temp : files) {
			if (temp.endsWith("Entity.java") == false) {
				continue;
			}
			System.out.println("----------" + temp + "--------");
			// entity文件的类名
			String className = temp.replace(".java", "");
			// dao文件的类名
			String daoName = temp.replace("Entity.java", "Dao");
			String fileName = temp.replace("Entity.java", "Dao.java");
			File targetFile = new File(rootPath + "/src/main/java/cn/com/platform/dao/" + fileName);

			if (targetFile.exists() == false) {
				List<String> stringLines = Lists.newArrayList();
				stringLines.add("package cn.com.platform.dao;");
				stringLines.add("import org.springframework.data.jpa.repository.JpaRepository;");
				stringLines.add("import org.springframework.data.jpa.repository.JpaSpecificationExecutor;");
				stringLines.add("import cn.com.platform.entity." + className + ";");
				stringLines.add("/**");
				stringLines.add(" *  ");
				stringLines.add(" * ");
				stringLines.add(" * @author nibili 2017年10月17日");
				stringLines.add(" */");
				stringLines.add("public interface " + daoName + " extends JpaRepository<" + className + ", Long>, JpaSpecificationExecutor<" + className + "> {");
				stringLines.add("}");
				FileUtils.writeLines(targetFile, stringLines);
			}
		}
		System.out.println("----------end--------");
	}
}
